import request from '@/utils/request'
import { getDictionaryDataSelector, updateDictionaryState } from '@/api/systemData/dictionary'
import { synchronousJiaXiao } from '@/api/campus/campus'

import StatusBox from '../renewal/StatusBox'
import RecordsHandleForm from '../insurance/Records/index.vue'
import CloseSupportForm from '../supportClose/Form/index.vue'

export default {
  name: 'campus',
  components: { StatusBox, RecordsHandleForm, CloseSupportForm },
  data() {
    return {
      showAll: false,
      query: {
        brand: 'bell',
        enCode: undefined,
        area: undefined,
        type: undefined,
        stateMark: undefined,
        openDate: undefined,
        businessName: undefined,
        contact: undefined,
        serviceEndDate: undefined,
        inurance: undefined,
        inuranceDate: [],
        pageType: '2'
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      },
      formVisible: false,
      exportBoxVisible: false,
      statusVisible: false,
      closeSupportVisible: false,
      recordsVisible: false,
      recordsHandleVisible: false,
      columnList: [{ prop: 'brand', label: '校区品牌' }, { prop: 'code', label: '校区编码' }, { prop: 'fullName', label: '校区全称' }, { prop: 'investors', label: '投资人' }, { prop: 'shortName', label: '校区简称' }, { prop: 'telephone', label: '固定电话' }, { prop: 'email', label: '校区邮箱' }, { prop: 'siteArea', label: '场地面积' }, { prop: 'manager', label: '负责人' }, { prop: 'managerMobile', label: '电话' }, { prop: 'wechat', label: '微信' }, { prop: 'provinceId', label: '省份' }, { prop: 'cityId', label: '城市' }, { prop: 'type', label: '校区类型' }, { prop: 'status', label: '校区状态' }, { prop: 'openDate', label: '开业日期' }, { prop: 'startDate', label: '服务开始日期' }, { prop: 'endDate', label: '服务截止日期' }, { prop: 'insurance', label: '保险备注' }, { prop: 'insuranceNo', label: '保险单号' }, { prop: 'insuranceDate', label: '保险到期日' }, { prop: 'businessName', label: '营业执照主体' }, { prop: 'businessNo', label: '营业执照号' }],
      typeOptions: [],
      statusOptions: [],
      brandOptions: [],
      levelOptions: [],
      inuranceOptions: [
        {
          enCode: '1',
          fullName: '未参保'
        },
        {
          enCode: '2',
          fullName: '已参保'
        },
        {
          enCode: '3',
          fullName: '已过期'
        }
      ]
    }
  },
  computed: {},
  created() {
    this.initData()
    this.gettypeOptions()
    this.getstateMarkOptions()
    this.getBrandOptions()
    this.getLevelOptions()
  },
  methods: {
    getBrandOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
        this.brandOptions = res
      })
    },
    getLevelOptions() {
      getDictionaryDataSelector('CampusLevel').then(res => {
        this.levelOptions = res.data.list
      })
    },
    gettypeOptions() {
      getDictionaryDataSelector('CampusType').then(res => {
        this.typeOptions = res.data.list
      })
    },
    getstateMarkOptions() {
      getDictionaryDataSelector('CampusStatus').then(res => {
        this.statusOptions = res.data.list
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `api/campus/transfer/list`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },

    handleSelectionChange(val) {
      const res = val.map(item => item.id)
      this.multipleSelection = res
    },
    addOrUpdateHandle(id, isDetail) {
      if (!this.multipleSelection.length) {
        this.$message({
          type: 'error',
          message: '请选择一条数据',
          duration: 1500
        })
        return
      }
      const ids = this.multipleSelection
      this.$confirm('您确定要操作这些数据吗, 是否继续？', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/confirm/transfer`,
            method: 'POST',
            data: {
              ids: ids
            }
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    formattedInfo(info) {
      if (info != '') {
        const people = info.split('/')
        return people
          .map(person => {
            const [name, phone] = person.split('(')
            if (phone != undefined && phone.length > 7) {
              const prefix = phone.slice(0, 3)
              const suffix = phone.slice(7)
              return `${name}(${prefix}****${suffix}`
            } else {
              return people
            }
          })
          .join('/')
      }
    },
    reset() {
      this.query = {
        brand: 'bell',
        enCode: undefined,
        area: undefined,
        type: undefined,
        stateMark: undefined,
        openDate: undefined,
        businessName: undefined,
        contact: undefined,
        endDate: undefined,
        inurance: undefined,
        inuranceDate: [],
        pageType: '2'
      }

      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    }
  }
}
